package com.xinqi.modules.databank.v1.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.databank.v1.domain.CourseInteractionSubmitEntity;
import com.xinqi.modules.databank.dto.req.InteractionSubmitQueryDTO;
import com.xinqi.modules.databank.dto.rsp.CourseInteractionSubmitResult;
import com.xinqi.modules.databank.v1.mapper.CourseInteractionSubmitMapper;
import com.xinqi.modules.databank.v1.manager.CourseInteractionSubmitManager;
import com.xinqi.modules.databank.v2.convert.CourseInteractionSubmitConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 学生答题记录-业务层
 *
 * @author dzy
 */
@Service
@RequiredArgsConstructor
public class CourseInteractionSubmitManagerImpl extends BaseServiceImpl<CourseInteractionSubmitMapper, CourseInteractionSubmitEntity> implements CourseInteractionSubmitManager {
    private final CourseInteractionSubmitMapper courseInteractionSubmitMapper;
    private final CourseInteractionSubmitConvert courseInteractionSubmitConvert;

    @Override
    public List<CourseInteractionSubmitResult> findListByContentId(Long contentId) {
        LambdaQueryWrapper<CourseInteractionSubmitEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseInteractionSubmitEntity::getContentId, contentId);
        List<CourseInteractionSubmitEntity> submitEntities = courseInteractionSubmitMapper.selectList(queryWrapper);
        return submitEntities.stream().map(courseInteractionSubmitConvert::convert).collect(Collectors.toList());
    }

    /**
     * 查询学生互动环节答题数据
     * @author dzy
     */
    @Override
    public List<CourseInteractionSubmitEntity> findStudentAnswer(Long contentId, Long partId, Long userId) {
        LambdaQueryWrapper<CourseInteractionSubmitEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseInteractionSubmitEntity::getIsReply, YesNoNumberEnum.YES.getCode());
        if (Objects.nonNull(contentId)) {
            wrapper.eq(CourseInteractionSubmitEntity::getContentId, contentId);
        }
        if (Objects.nonNull(partId)) {
            wrapper.eq(CourseInteractionSubmitEntity::getResourcePartId, partId);
        }
        if (Objects.nonNull(userId)) {
            wrapper.eq(CourseInteractionSubmitEntity::getUserId, userId);
        }
        return list(wrapper);
    }

    /**
     * 查询学生互动环节答题数据
     * @author dzy
     */
    @Override
    public List<CourseInteractionSubmitResult> studentAnswerList(InteractionSubmitQueryDTO query) {
        return courseInteractionSubmitMapper.studentAnswerList(query);
    }

}
